package com.fireline.beauty.cashier;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.bean.HyxfxmVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.fireline.beauty.seting.FwxmTextPopup;
import com.zgw.fireline.base.common.Assert;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;
import com.zgw.fireline.base.widgets.ControlUtil;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

/**
 * 会员消费添加
 * */
public class HyxfEdit implements IFunction {

	private Shell shell;
	private Text textXfje;
	private Text textBz;
	private Text textHlmc;
	private Table tableHlxm;
	private Text text_4;
	private Text textCpmc;
	private Table tableCpxm;
	private CLabel labXfdh;
	private CLabel labHyxm;
	private CLabel labKnye;
	private final String hybh;
	/**
	 * @wbp.nonvisual location=27,592
	 */
	private final Dataset hytcSet = new Dataset(true, "hytcxm",
			SqliteJdbcImpl.INSTANCE);
	private DataTable tableTcxm;
	private Label labHlzj;
	private Label labCpzj;
	private Label labTczj;
	private Label labZj;
	/**
	 * @wbp.nonvisual location=23,662
	 */
	private final Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=97,602
	 */
	private final Dataset hyxxSet2 = new Dataset(
			"select * from hyxx where hybh=?", SqliteJdbcImpl.INSTANCE);
	private Button btnXj;
	private Button btnCzk;
	private Button btnXyk;
	private Button btnDjq;
	private Button btnLhjz;
	private Button btnQk;
	private Composite content;

	public int result;
	private final List<HyxfxmVo> gdxmList; // 帐单消费项目

	public HyxfEdit(String hybh) {
		this.hybh = hybh;
		gdxmList = null;
	}

	/**
	 * 处理会员挂单明细
	 * 
	 * @param hybh
	 *            会员编号
	 * @param list
	 *            帐单内容明细数据
	 * */
	public HyxfEdit(String hybh, List<HyxfxmVo> list) {
		this.hybh = hybh;
		this.gdxmList = list;
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		hytcSet.setKeyColumns(new String[] { "xmbh" });

		hyxxSet2.setKeyColumns(new String[] {});
		Composite content = new Composite(parent, SWT.NONE);
		GridLayout gl_content = new GridLayout(1, false);
		gl_content.marginHeight = 10;
		gl_content.marginWidth = 10;
		content.setLayout(gl_content);
		Group group = new Group(content, SWT.SHADOW_ETCHED_IN);
		GridLayout gl_group = new GridLayout(6, false);
		gl_group.horizontalSpacing = 0;
		gl_group.verticalSpacing = 0;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("基本信息");
		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("消费单号：");

		labXfdh = new CLabel(group, SWT.NONE);
		labXfdh.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labXfdh.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		GridData gd_labXfdh = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_labXfdh.widthHint = 150;
		labXfdh.setLayoutData(gd_labXfdh);

		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("会员姓名：");

		labHyxm = new CLabel(group, SWT.NONE);
		labHyxm.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labHyxm.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		GridData gd_labHyxm = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_labHyxm.widthHint = 80;
		labHyxm.setLayoutData(gd_labHyxm);

		Label label_2 = new Label(group, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_2.setText("卡内佘额：");

		labKnye = new CLabel(group, SWT.NONE);
		labKnye.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labKnye.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		GridData gd_labKnye = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_labKnye.widthHint = 80;
		labKnye.setLayoutData(gd_labKnye);

		Group group_1 = new Group(content, SWT.SHADOW_ETCHED_IN);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		group_1.setText("消费清单");
		group_1.setLayout(new GridLayout(1, false));

		TabFolder tabFolder = new TabFolder(group_1, SWT.NONE);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("护理消费");

		Composite composite_4 = new Composite(tabFolder, SWT.NONE);
		tabItem.setControl(composite_4);
		GridLayout gl_composite_4 = new GridLayout(5, false);
		gl_composite_4.horizontalSpacing = 0;
		composite_4.setLayout(gl_composite_4);

		Label label_12 = new Label(composite_4, SWT.NONE);
		GridData gd_label_12 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_label_12.horizontalIndent = 20;
		label_12.setLayoutData(gd_label_12);
		label_12.setText("项目编号或名称：");

		textHlmc = new Text(composite_4, SWT.BORDER);
		GridData gd_textHlmc = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_textHlmc.widthHint = 100;
		textHlmc.setLayoutData(gd_textHlmc);
		final FwxmTextPopup fwxmPopup = new FwxmTextPopup(textHlmc,
				new Listener() {
					@Override
					public void handleEvent(Event event) {
						doAddHlxm((Dataset) event.data);
					}
				});

		Label lblNewLabel_6 = new Label(composite_4, SWT.NONE);
		lblNewLabel_6.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				fwxmPopup.show("");
			}
		});
		lblNewLabel_6.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		GridData gd_lblNewLabel_6 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_6.horizontalIndent = 5;
		lblNewLabel_6.setLayoutData(gd_lblNewLabel_6);
		lblNewLabel_6.setImage(SWTResourceManager.getImage(HyxfEdit.class,
				"/com/fireline/beauty/image/query.png"));
		lblNewLabel_6.setToolTipText("查找");

		Button btnNewButton_3 = new Button(composite_4, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEditHlxm();
			}
		});
		GridData gd_btnNewButton_3 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_3.widthHint = 50;
		gd_btnNewButton_3.horizontalIndent = 20;
		btnNewButton_3.setLayoutData(gd_btnNewButton_3);
		btnNewButton_3.setText("修改");

		Button btnNewButton_4 = new Button(composite_4, SWT.NONE);
		GridData gd_btnNewButton_4 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_4.widthHint = 50;
		gd_btnNewButton_4.horizontalIndent = 6;
		btnNewButton_4.setLayoutData(gd_btnNewButton_4);
		btnNewButton_4.setText("删除");

		tableHlxm = new Table(composite_4, SWT.BORDER | SWT.FULL_SELECTION);
		tableHlxm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5,
				1));
		tableHlxm.setHeaderVisible(true);
		tableHlxm.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(tableHlxm, SWT.NONE);
		tableColumn.setWidth(71);
		tableColumn.setText("项目编号");

		TableColumn tableColumn_1 = new TableColumn(tableHlxm, SWT.NONE);
		tableColumn_1.setWidth(98);
		tableColumn_1.setText("项目名称");

		TableColumn tableColumn_2 = new TableColumn(tableHlxm, SWT.RIGHT);
		tableColumn_2.setWidth(70);
		tableColumn_2.setText("原价");

		TableColumn tableColumn_3 = new TableColumn(tableHlxm, SWT.RIGHT);
		tableColumn_3.setWidth(70);
		tableColumn_3.setText("优惠价");

		TableColumn tableColumn_4 = new TableColumn(tableHlxm, SWT.RIGHT);
		tableColumn_4.setWidth(43);
		tableColumn_4.setText("次数");

		TableColumn tableColumn_5 = new TableColumn(tableHlxm, SWT.RIGHT);
		tableColumn_5.setWidth(60);
		tableColumn_5.setText("合计");

		TableColumn tableColumn_6 = new TableColumn(tableHlxm, SWT.NONE);
		tableColumn_6.setWidth(47);
		tableColumn_6.setText("积分");
		new Label(composite_4, SWT.NONE);
		new Label(composite_4, SWT.NONE);
		new Label(composite_4, SWT.NONE);
		new Label(composite_4, SWT.NONE);
		new Label(composite_4, SWT.NONE);

		TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("产品消费");

		Composite composite_5 = new Composite(tabFolder, SWT.NONE);
		tabItem_1.setControl(composite_5);
		GridLayout gl_composite_5 = new GridLayout(5, false);
		gl_composite_5.horizontalSpacing = 0;
		composite_5.setLayout(gl_composite_5);

		Label label_13 = new Label(composite_5, SWT.NONE);
		GridData gd_label_13 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_label_13.horizontalIndent = 20;
		label_13.setLayoutData(gd_label_13);
		label_13.setText("项目编号或名称：");

		textCpmc = new Text(composite_5, SWT.BORDER);
		GridData gd_textCpmc = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_textCpmc.widthHint = 100;
		textCpmc.setLayoutData(gd_textCpmc);
		final CpxxTextPopup popupCp = new CpxxTextPopup(textCpmc,
				new Listener() {
					@Override
					public void handleEvent(Event event) {
						doAddCp((Dataset) event.data);
					}
				});
		Label label_14 = new Label(composite_5, SWT.NONE);
		label_14.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				popupCp.show("");
			}
		});
		label_14.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		GridData gd_label_14 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_14.horizontalIndent = 5;
		label_14.setLayoutData(gd_label_14);
		label_14.setToolTipText("查找");
		label_14.setImage(SWTResourceManager.getImage(HyxfEdit.class,
				"/com/fireline/beauty/image/query.png"));

		Button button_4 = new Button(composite_5, SWT.NONE);
		GridData gd_button_4 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_4.widthHint = 50;
		gd_button_4.horizontalIndent = 20;
		button_4.setLayoutData(gd_button_4);
		button_4.setText("修改");
		button_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEditCpxm();
			}
		});
		Button button_5 = new Button(composite_5, SWT.NONE);
		GridData gd_button_5 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_5.widthHint = 50;
		gd_button_5.horizontalIndent = 6;
		button_5.setLayoutData(gd_button_5);
		button_5.setText("删除");

		tableCpxm = new Table(composite_5, SWT.BORDER | SWT.FULL_SELECTION);
		tableCpxm.setLinesVisible(true);
		tableCpxm.setHeaderVisible(true);
		tableCpxm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5,
				1));

		TableColumn tableColumn_7 = new TableColumn(tableCpxm, SWT.NONE);
		tableColumn_7.setWidth(71);
		tableColumn_7.setText("产品编号");

		TableColumn tableColumn_8 = new TableColumn(tableCpxm, SWT.NONE);
		tableColumn_8.setWidth(98);
		tableColumn_8.setText("产品名称");

		TableColumn tableColumn_9 = new TableColumn(tableCpxm, SWT.RIGHT);
		tableColumn_9.setWidth(70);
		tableColumn_9.setText("原价");

		TableColumn tableColumn_10 = new TableColumn(tableCpxm, SWT.RIGHT);
		tableColumn_10.setWidth(70);
		tableColumn_10.setText("优惠价");

		TableColumn tableColumn_11 = new TableColumn(tableCpxm, SWT.RIGHT);
		tableColumn_11.setWidth(43);
		tableColumn_11.setText("数量");

		TableColumn tableColumn_12 = new TableColumn(tableCpxm, SWT.RIGHT);
		tableColumn_12.setWidth(60);
		tableColumn_12.setText("合计");

		TableColumn tableColumn_13 = new TableColumn(tableCpxm, SWT.NONE);
		tableColumn_13.setWidth(47);
		tableColumn_13.setText("积分");
		new Label(composite_5, SWT.NONE);
		new Label(composite_5, SWT.NONE);
		new Label(composite_5, SWT.NONE);
		new Label(composite_5, SWT.NONE);
		new Label(composite_5, SWT.NONE);

		TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
		tabItem_2.setText("套餐使用");

		Composite composite_8 = new Composite(tabFolder, SWT.NONE);
		tabItem_2.setControl(composite_8);
		GridLayout gl_composite_8 = new GridLayout(5, false);
		gl_composite_8.horizontalSpacing = 0;
		composite_8.setLayout(gl_composite_8);

		Label label_15 = new Label(composite_8, SWT.NONE);
		GridData gd_label_15 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_label_15.horizontalIndent = 20;
		label_15.setLayoutData(gd_label_15);
		label_15.setText("项目编号或名称：");

		text_4 = new Text(composite_8, SWT.BORDER);
		GridData gd_text_4 = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_text_4.widthHint = 100;
		text_4.setLayoutData(gd_text_4);

		Label label_16 = new Label(composite_8, SWT.NONE);
		label_16.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		GridData gd_label_16 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_16.horizontalIndent = 5;
		label_16.setLayoutData(gd_label_16);
		label_16.setToolTipText("查找");
		label_16.setImage(SWTResourceManager.getImage(HyxfEdit.class,
				"/com/fireline/beauty/image/query.png"));

		Button button_6 = new Button(composite_8, SWT.NONE);
		GridData gd_button_6 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_6.widthHint = 50;
		gd_button_6.horizontalIndent = 20;
		button_6.setLayoutData(gd_button_6);
		button_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEditTcxf();
			}
		});
		button_6.setText("修改");

		Button button_7 = new Button(composite_8, SWT.NONE);
		GridData gd_button_7 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_7.widthHint = 50;
		gd_button_7.horizontalIndent = 6;
		button_7.setLayoutData(gd_button_7);
		button_7.setText("删除");

		tableTcxm = new DataTable(composite_8, SWT.BORDER | SWT.FULL_SELECTION,
				hytcSet);
		tableTcxm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5,
				1));
		tableTcxm.setHeaderVisible(true);
		tableTcxm.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(tableTcxm,
				SWT.NONE, "xmbh");
		dataTableColumn.setText("项目编号");
		dataTableColumn.setWidth(80);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(tableTcxm,
				SWT.NONE, "xmmc");
		dataTableColumn_2.setText("项目名称");
		dataTableColumn_2.setWidth(100);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(tableTcxm,
				SWT.CENTER, "sycs");
		dataTableColumn_1.setText("卡内剩余次数");
		dataTableColumn_1.setWidth(90);

		TableColumn tableColumn_14 = new TableColumn(tableTcxm, SWT.CENTER);
		tableColumn_14.setWidth(200);
		tableColumn_14.setText("本次消费次数");
		new Label(composite_8, SWT.NONE);
		new Label(composite_8, SWT.NONE);
		new Label(composite_8, SWT.NONE);
		new Label(composite_8, SWT.NONE);
		new Label(composite_8, SWT.NONE);

		Composite composite = new Composite(group_1, SWT.NONE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.spacing = 0;
		composite.setLayout(rl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setText("护理合计：");

		labHlzj = new Label(composite, SWT.NONE);
		labHlzj.setLayoutData(new RowData(70, SWT.DEFAULT));
		labHlzj.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labHlzj.setText("New Label");

		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setText("产品合计：");

		labCpzj = new Label(composite, SWT.NONE);
		labCpzj.setLayoutData(new RowData(70, SWT.DEFAULT));
		labCpzj.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labCpzj.setText("New Label");

		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setText("套餐合计：");

		labTczj = new Label(composite, SWT.NONE);
		labTczj.setLayoutData(new RowData(70, SWT.DEFAULT));
		labTczj.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labTczj.setText("New Label");

		Label label_7 = new Label(composite, SWT.NONE);
		label_7.setText("总计：");

		labZj = new Label(composite, SWT.NONE);
		labZj.setLayoutData(new RowData(70, SWT.DEFAULT));
		labZj.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labZj.setText("New Label");

		Label label_9 = new Label(group_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));

		Composite composite_2 = new Composite(group_1, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(3, false);
		gl_composite_2.marginWidth = 15;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		Label lblNewLabel_5 = new Label(composite_2, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_5.setText("本次消费共：");

		textXfje = new Text(composite_2, SWT.BORDER);
		textXfje.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textXfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_textXfje = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_textXfje.widthHint = 100;
		textXfje.setLayoutData(gd_textXfje);
		ControlUtil.setCurrencyRule(textXfje, false, false);
		Label label_10 = new Label(composite_2, SWT.NONE);
		label_10.setText("元");

		Label label_11 = new Label(composite_2, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_11.setText("备 注 信息：");

		textBz = new Text(composite_2, SWT.BORDER);
		textBz.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1));
		Composite composite_3 = new Composite(group_1, SWT.NONE);
		RowLayout rl_composite_3 = new RowLayout(SWT.HORIZONTAL);
		rl_composite_3.pack = false;
		rl_composite_3.justify = true;
		composite_3.setLayout(rl_composite_3);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		btnXj = new Button(composite_3, SWT.RADIO);
		btnXj.setSelection(true);
		btnXj.setText("现金");

		btnCzk = new Button(composite_3, SWT.RADIO);
		btnCzk.setText("储值卡");

		btnXyk = new Button(composite_3, SWT.RADIO);
		btnXyk.setText("信用卡");

		btnDjq = new Button(composite_3, SWT.RADIO);
		btnDjq.setText("代金券");

		btnLhjz = new Button(composite_3, SWT.RADIO);
		btnLhjz.setText("联合结帐");

		btnQk = new Button(composite_3, SWT.RADIO);
		btnQk.setText("欠款");

		Composite composite_1 = new Composite(content, SWT.NONE);
		composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));

		Button btnNewButton_2 = new Button(composite_1, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnNewButton_2.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton_2.setText("挂单");
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave(true);
			}
		});
		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		btnNewButton_1.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton_1.setText("收费确定");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave(false);
			}
		});

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				HyxfEdit.this.content.dispose();
			}
		});
		btnNewButton.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton.setText("取消");
		setParams();
		createGUIAfter();
		setValues();
		initizlizeGdxx();
		this.content = content;
		return content;
	}

	/*
	 * 初始化挂单信息
	 */
	private void initizlizeGdxx() {
		if (gdxmList == null) {
			return;
		}
		for (HyxfxmVo xm : gdxmList) {
			if (xm.getXmlx() == 3) {
				for (int i = 0; i < hytcSet.getCount(); i++) {
					hytcSet.setSelected(xm.xmbh);
					fillItem(xm, tableTcxm.getSelection()[0]);
				}
			} else {
				fillItem(xm, null);
			}
		}
	}

	/*
	 * @param gd 是否挂单
	 */
	protected void doSave(boolean gd) {
		String xj = "0", czk = "0", djq = "0", xyk = "0", jf = "0";
		String gz = "0"; // 挂帐欠款
		int flag = 1; // 状态：1正常结账0挂单
		String rq = SimpleDateFormat.getDateTimeInstance().format(new Date());
		String dqje = hyxxSet2.getString("knje");
		if (gd) {
			if (textBz.getText().trim().equals("")) {
				MessageDialog.openInformation(shell, "提示", "挂单必须填写备注信息");
				return;
			}
			flag = 0;
		} else if (btnXj.getSelection()) {// 弹出现金付款窗口
			XjfkDialog dlg = new XjfkDialog(shell, textXfje.getText());
			if (dlg.open() == SWT.OK) {
				xj = textXfje.getText();
			} else {
				return;
			}
		} else if (btnCzk.getSelection()) {
			CzkfkDialog dlg = new CzkfkDialog(shell, textXfje.getText(), dqje);
			if (dlg.open() == SWT.OK) {
				czk = textXfje.getText();
			} else {
				return;
			}
		} else if (btnDjq.getSelection()) {
			XykfkDialog dlg = new XykfkDialog(shell, textXfje.getText(), 2);
			if (dlg.open() == SWT.OK) {
				xyk = textXfje.getText();
			} else {
				return;
			}
		} else if (btnXyk.getSelection()) {
			XykfkDialog dlg = new XykfkDialog(shell, textXfje.getText(), 1);
			if (dlg.open() == SWT.OK) {
				xyk = textXfje.getText();
			} else {
				return;
			}
		} else if (btnLhjz.getSelection()) {
			LhjzDialog dlg = new LhjzDialog(shell, textXfje.getText(),
					hyxxSet2.getString("knje"));
			if (dlg.open() == SWT.OK) {
				xj = dlg.xj;
				czk = dlg.czk;
				xyk = dlg.xyk;
				djq = dlg.djq;
			} else {
				return;
			}
		} else if (btnQk.getSelection()) {
			QkfkDialog dlg = new QkfkDialog(shell, textXfje.getText());
			if (dlg.open() == SWT.OK) {
				xj = dlg.xj;
				czk = dlg.czk;
				xyk = dlg.xyk;
				djq = dlg.djq;
				gz = dlg.qkje;
			} else {
				return;
			}
		}
		// 计算当前卡剩余金额
		dqje = new BigDecimal(dqje).subtract(new BigDecimal(czk)).toString();

		/*
		 * 新增消费记录
		 */
		BatchItem batchItem = new BatchItem(batch);
		batchItem
				.setCommand("\r\n\tINSERT INTO hyxf(xfid, hybh, rq, xfje, xj, czk, djq, xyk, gz, jf, czy, flag, bz, fd)\r\nVALUES(? /*xfid*/,?/* hybh*/, ? /*rq*/, ?/*xfje*/, ?/*xj*/, ?/*czk*/,\r\n ?/*djq*/,?/* xyk*/,?/*gz,*/,? /*jf*/, ? /*czy*/, ? /*flag*/,? /*bz*/,? /*fd*/)");
		batchItem.setName("新增消费记录");
		batchItem.setParam(0, labXfdh.getText());
		batchItem.setParam(1, hyxxSet2.getValue("hybh"));
		batchItem.setParam(2, rq);
		batchItem.setParam(3, textXfje.getText());
		batchItem.setParam(4, xj);
		batchItem.setParam(5, czk);
		batchItem.setParam(6, djq);
		batchItem.setParam(7, xyk);
		batchItem.setParam(8, gz);
		batchItem.setParam(9, "0");
		batchItem.setParam(10, ContextHelp.getUserName());
		batchItem.setParam(11, flag);
		batchItem.setParam(12, textBz.getText());
		batchItem.setParam(13, "01");

		List<HyxfxmVo> xfxmList = new ArrayList<HyxfxmVo>();

		/*
		 * 添加护理消费
		 */

		for (TableItem item : tableHlxm.getItems()) {
			xfxmList.add((HyxfxmVo) item.getData());
		}
		/*
		 * 添加产品消费
		 */
		for (TableItem item : tableCpxm.getItems()) {
			xfxmList.add((HyxfxmVo) item.getData());
		}
		/*
		 * 添加套餐消费
		 */
		for (TableItem item : tableTcxm.getItems()) {
			HyxfxmVo xm = (HyxfxmVo) item.getData();
			if (xm != null && xm.cs > 0)
				xfxmList.add((HyxfxmVo) item.getData());
		}

		BatchItem addItem;
		for (HyxfxmVo xm : xfxmList) {
			addItem = new BatchItem(batch);
			addItem.setName("添加会员消费项目记录");
			addItem.setCommand("INSERT INTO hyxfnr ( xfid, lx,bh, cs, dj, cw, dqcs, jf, flag, fd, ygbh) "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?)");
			addItem.setParam(0, labXfdh.getText());
			addItem.setParam(1, xm.getXmlx());// lx 消费类型： 1 护理 2 产品 3 套餐
			addItem.setParam(2, xm.xmbh);
			addItem.setParam(3, xm.cs);
			addItem.setParam(4, xm.dj);
			addItem.setParam(5, xm.cw);
			addItem.setParam(6, xm.dqcs);
			addItem.setParam(7, xm.jf);
			addItem.setParam(8, flag);
			addItem.setParam(9, "01");//
			addItem.setParam(10, xm.ygbh); // 员工编号

			// 添加套餐 次数变动记录
			if (xm.getXmlx() == 3 && !gd) {
				BatchItem yebd = new BatchItem(batch);
				yebd.setName("添加套餐消费变动记录");
				yebd.setCommand("INSERT INTO yebd( hybh, lx, rq, je_inc, je_dec, cs_inc, cs_dec, sycs, syje, czy, bz, xmbh, fd, xfid)"
						+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
				yebd.setParam(0, hybh);
				yebd.setParam(1, "套餐消费");
				yebd.setParam(2, rq);
				yebd.setParam(3, "0");
				yebd.setParam(4, "0");
				yebd.setParam(5, "0");
				yebd.setParam(6, xm.cs);
				yebd.setParam(7, xm.dqcs);
				yebd.setParam(8, dqje);
				yebd.setParam(9, ContextHelp.getUserName());
				yebd.setParam(10, "消费单号:" + labXfdh.getText());
				yebd.setParam(11, xm.xmmc);
				yebd.setParam(12, "");
				yebd.setParam(13, labXfdh.getText());
			}
		}
		if (!gd && Double.parseDouble(czk) > 0) {
			// 添加佘额变动记录
			BatchItem yebd = new BatchItem(batch);
			yebd.setName("添加储值消费佘额变动记录");
			yebd.setCommand("INSERT INTO yebd( hybh, lx, rq, je_inc, je_dec, cs_inc, cs_dec, sycs, syje, czy, bz, xmbh, fd, xfid)"
					+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			yebd.setParam(0, hybh);
			yebd.setParam(1, "储值消费");
			yebd.setParam(2, rq);
			yebd.setParam(3, "0");
			yebd.setParam(4, czk);
			yebd.setParam(5, "0");
			yebd.setParam(6, "0");
			yebd.setParam(7, "0");
			yebd.setParam(8, dqje);
			yebd.setParam(9, ContextHelp.getUserName());
			yebd.setParam(10, "消费单号:" + labXfdh.getText());
			yebd.setParam(11, "");
			yebd.setParam(12, "");
			yebd.setParam(13, labXfdh.getText());

		}
		if (!gd) {
			// 修改会员信息 消费金额累积
			BatchItem xghyxx = new BatchItem(batch);
			xghyxx.setName("修改会员信息");
			xghyxx.setCommand("update hyxx set xfje=xfje+?,knje=knje-?,hyjf=hyjf+?");
			xghyxx.setParam(0, textXfje.getText());
			xghyxx.setParam(1, czk); // 减储值卡消费额
			xghyxx.setParam(2, jf);
		}
		batch.execute();
		result = SWT.OK;
		content.dispose();
	}

	// 计算应收总金额
	private void countzj() {
		// 护理总计
		BigDecimal hlzj = new BigDecimal(0);
		for (TableItem item : tableHlxm.getItems()) {
			HyxfxmVo xm = (HyxfxmVo) item.getData();
			hlzj = hlzj.add(new BigDecimal(xm.getZje()));
		}
		// 产品总计
		BigDecimal cpzj = new BigDecimal(0);
		for (TableItem item : tableCpxm.getItems()) {
			HyxfxmVo xm = (HyxfxmVo) item.getData();
			cpzj = cpzj.add(new BigDecimal(xm.getZje()));
		}
		// 使用套餐数
		BigDecimal tczj = new BigDecimal(0);
		for (TableItem item : tableTcxm.getItems()) {
			HyxfxmVo xm = (HyxfxmVo) item.getData();
			if (xm != null)
				tczj = tczj.add(new BigDecimal(xm.cs));
		}
		// 消费总计
		BigDecimal xfzj = hlzj.add(cpzj);
		DecimalFormat format = new DecimalFormat("0.00元");
		labHlzj.setText(format.format(hlzj));
		labCpzj.setText(format.format(cpzj));
		labTczj.setText(tczj.toString());
		labZj.setText(format.format(xfzj));
		textXfje.setText(xfzj.toString());
	}

	protected void doEditTcxf() {
		if (hytcSet.getSelectedIndex() == -1)
			return;

		TableItem item = tableTcxm.getSelection()[0];
		TjTcxfDialog dlg;
		if (item.getData() == null) {
			dlg = new TjTcxfDialog(shell, hytcSet.getString("xmbh"), hybh);
		} else {
			dlg = new TjTcxfDialog(shell, (HyxfxmVo) item.getData(), hybh);
		}
		HyxfxmVo xm = dlg.open();
		if (xm != null)
			fillItem(xm, item);
	}

	/*
	 * 修改产品项目
	 */
	protected void doEditCpxm() {
		if (tableCpxm.getSelectionCount() > 0) {
			TableItem item = tableCpxm.getSelection()[0];
			HyxfxmVo cpxm = (HyxfxmVo) item.getData();
			TjCpxfDialog dlg = new TjCpxfDialog(shell, cpxm);
			if (dlg.open() != null) {
				fillItem(cpxm, item);
			}
		}
	}

	/*
	 * 添加产品项目
	 */
	protected void doAddCp(Dataset cpxm) {
		TjCpxfDialog dlg = new TjCpxfDialog(shell, cpxm.getString("cpbh"));
		HyxfxmVo xm;
		if ((xm = dlg.open()) != null) {
			fillItem(xm, null);
		}
	}

	protected void doEditHlxm() {
		if (tableHlxm.getSelectionCount() > 0) {
			TableItem item = tableHlxm.getSelection()[0];
			HyxfxmVo hlxm = (HyxfxmVo) item.getData();
			TjHlxfDialog dlg = new TjHlxfDialog(shell, hlxm);
			if (dlg.open() != null) {
				fillItem(hlxm, item);
			}
		}
	}

	protected void doAddHlxm(Dataset fwxm) {
		TjHlxfDialog dlg = new TjHlxfDialog(shell, fwxm.getString("xmbh"));
		HyxfxmVo xm;
		if ((xm = dlg.open()) != null) {
			fillItem(xm, null);
		}
	}

	/**
	 * 将消费项目对象填充至 相对应的表格
	 * 
	 * @param target
	 *            填充的目标行，如果需要新增一行target则传空值
	 */
	public void fillItem(HyxfxmVo xm, TableItem target) {
		TableItem item = null;
		if (xm.getXmlx() == 1) {
			if (target != null) {
				Assert.isTrue(target.getParent() == tableHlxm);
				item = target;
			} else {
				item = new TableItem(tableHlxm, SWT.NONE);
			}
			item.setText(0, xm.xmbh);
			item.setText(1, xm.xmmc);
			item.setText(2, xm.yj);
			item.setText(3, xm.dj);
			item.setText(4, String.valueOf(xm.cs));
			item.setText(5, String.valueOf(xm.getZje()));
			item.setText(6, xm.jf == null ? "0" : xm.jf);// 积分
			item.setData(xm);
			tableHlxm.setSelection(item);
		} else if (xm.getXmlx() == 2) {
			if (target != null) {
				Assert.isTrue(target.getParent() == tableCpxm);
				item = target;
			} else {
				item = new TableItem(tableCpxm, SWT.NONE);
			}
			item.setText(0, xm.xmbh);
			item.setText(1, xm.xmmc);
			item.setText(2, xm.yj);
			item.setText(3, xm.dj);
			item.setText(4, String.valueOf(xm.cs));
			item.setText(5, String.valueOf(xm.getZje()));
			item.setText(6, xm.jf == null ? "0" : xm.jf);// 积分
			item.setData(xm);
			tableCpxm.setSelection(item);
		} else if (xm.getXmlx() == 3) {
			Assert.isNotNull(target);
			Assert.isTrue(target.getParent() == tableTcxm);
			item = target;
			item.setText(3, String.valueOf(xm.cs));
			item.setData(xm);
			tableTcxm.setSelection(item);
		}
		countzj();
	}

	protected void setParams() {
		hytcSet.setParam(0, hybh);
		hyxxSet2.setParam(0, hybh);
	}

	protected void createGUIAfter() {
		hytcSet.update();
		hyxxSet2.update();
		hyxxSet2.setSelected(0);
	}

	protected void setValues() {
		labHyxm.setText(hyxxSet2.getValue("hyxm", String.class, ""));
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSS");
		labXfdh.setText("XF" + format.format(new Date()));
		labKnye.setText(hyxxSet2.getStringForExpr("${knje}元"));
		countzj();
	}
}
